
#include "widget.h"
#include "ui_widget.h"
#include<Qpainter>
#include<qrandom.h>
#include<QPixmap>


Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    //使用定时器
    timer=new QTimer();

    //信号槽发送定时器超时信号
    connect(timer,SIGNAL(timeout()),this,SLOT(timeout()));
    resize(976,536);
    //初始化蛇
    QRectF rect(488,268,nodeWidth,nodeHeight);
    snake.append(rect);
    up();
    up();
    //初始化食物
    addFood();


}

Widget::~Widget()
{
    delete ui;
}

//按键处理
void Widget::keyPressEvent(QKeyEvent*event)
{
    switch(event->key())
    {
        //不同按键
    case Qt::Key_Up:
        //防止向下时向上走自杀
        if(moveFlag!=DIR_DOWN){
        moveFlag=DIR_UP;
        }
        break;
    case Qt::Key_Down:
        if(moveFlag!=DIR_UP){
        moveFlag=DIR_DOWN;
        }
        break;
    case Qt::Key_Left:
        if(moveFlag!=DIR_RIGHT){
        moveFlag=DIR_LEFT;
        }
        break;
    case Qt::Key_Right:
        if(moveFlag!=DIR_LEFT){
        moveFlag=DIR_RIGHT;
        }
        break;
    case Qt::Key_Space:
        //暂停和开始
        if(gameStart==false)
        {
        gameStart=true;
        //启动定时器
        timer->start(100);
        }
        else
        {
        gameStart=false;
        timer->stop();
        }
        break;
    default:
        break;
    }

}
//蛇的移动
void Widget::timeout(){
    int count=1;//标记，判断重合
    //判断有没有重合
    if(snake[0].intersects(foodNode))
    {
        count++;//吃到奖品则增加两次
        addFood();
    }
    while(count--){//增加一次
    switch(moveFlag)
    {
    case DIR_UP:
        up();
        //在上面加一个。
        break;
    case DIR_DOWN:
        down();
        break;
    case DIR_LEFT:
        left();
        break;
    case DIR_RIGHT:
        right();
        break;
    default:
        break;
    }
    }
    lastRemove();//删除最后一节
    update();//刷新

}
void Widget::up(){
    QPointF leftTop=QPointF(snake[0].x(),snake[0].y()-nodeHeight);
    QPointF rightBotom=snake[0].bottomRight()+QPointF(0,nodeHeight);

    leftTop=QPointF(snake[0].x(),snake[0].y()-nodeHeight);
    rightBotom=snake[0].topRight();

    snake.insert(0,QRectF(leftTop,rightBotom));
}
void Widget::down()
{
    QPointF leftTop=snake[0].bottomLeft();
    QPointF rightBottom=snake[0].bottomRight()+QPointF(0,nodeHeight);

    leftTop=snake[0].bottomLeft();
    rightBottom=snake[0].bottomRight()+QPointF(0,nodeHeight);


    snake.insert(0,QRectF(leftTop,rightBottom));
}
void Widget::left()
{
    QPointF leftTop=snake[0].topLeft()-QPointF(nodeWidth,0);
    QPointF rightBottom=leftTop+QPointF(nodeWidth,nodeHeight);

    leftTop=snake[0].topLeft()-QPointF(nodeWidth,0);

    rightBottom=leftTop+QPointF(nodeWidth,nodeHeight);
    snake.insert(0,QRectF(leftTop,rightBottom));
}
void Widget::right()
{
    QPointF leftTop=snake[0].topRight();
    QPointF rightBottom=leftTop+QPointF(nodeWidth,nodeHeight);

    leftTop=snake[0].topRight();

     rightBottom=leftTop+QPointF(nodeWidth,nodeHeight);
    snake.insert(0,QRectF(leftTop,rightBottom));
}
//删除蛇
void Widget::lastRemove()
{
    snake.removeLast();
}
//增加食物
void Widget::addFood()
{
    foodNode=QRectF(rand()%(this->width()/19)*19,rand()%(this->height()/20)*20,
                        nodeWidth,nodeHeight);
}
//判断蛇的身体有没有重合
bool Widget::crashJudge()
{
    for(int i=1;i<snake.length();++i)
   {
    for(int j=1+i;j<snake.length();++j)
    {
    if(snake[0]==snake[i])
     {
        return true;
     }
    }
   }
    return false;
}
//图像处理部分
void Widget::paintEvent(QPaintEvent*event)
{
    QPainter painter(this);
    QPen pen;
    QBrush brush;
    //背景图片
    QPixmap pix;
    pix.load("D:/snake/background2");
    painter.drawPixmap(0,0,976,536,pix);
    //做出蛇
    pen.setColor(Qt::black);
    brush.setColor(Qt::green);
    brush.setStyle(Qt::TexturePattern);
    QPixmap photo("D:/snake/rainbow2");
    painter.setPen(pen);
    brush.setTexture(photo);
    painter.setBrush(brush);
    for(int i=0;i<snake.length();++i){

    painter.drawRect(snake[i]);

    }
    //画出食物

    brush.setColor(Qt::red);
    brush.setStyle(Qt::TexturePattern);
    QPixmap photo2("D:/snake/pink3");
    painter.setPen(pen);
    brush.setTexture(photo2);
    painter.setBrush(brush);
    painter.drawRect(foodNode);
    if(crashJudge())
    {
    //弹出字幕
    QFont font("微软雅黑",40,QFont::ExtraLight,false);
    painter.setFont(font);
    painter.drawText((this->width()-250)/2,
                     (this->height()-30)/2,
        QString("YOU DIED"));
    timer->stop();
    }
    //蛇撞墙死亡
    if(snake[0].x()+nodeWidth*1>this->width())
    {
    QFont font("微软雅黑",40,QFont::ExtraLight,false);
    painter.setFont(font);
    painter.drawText((this->width()-250)/2,
      (this->height()-30)/2,
      QString("YOU DIED"));
        timer->stop();
    }
    if(snake[0].x()<0)
    {
    QFont font("微软雅黑",40,QFont::ExtraLight,false);
    painter.setFont(font);
    painter.drawText((this->width()-250)/2,
      (this->height()-30)/2,
        QString("YOU DIED"));
               timer->stop();
    }
    if(snake[0].y()+nodeHeight*1>this->height())
    {
    QFont font("微软雅黑",40,QFont::ExtraLight,false);
   painter.setFont(font);
   painter.drawText((this->width()-250)/2,
                 (this->height()-30)/2,
            QString("YOU DIED"));
               timer->stop();
    }
    if(snake[0].y()<0)
    {
    QFont font("微软雅黑",40,QFont::ExtraLight,false);

    painter.setFont(font);
               painter.drawText((this->width()-250)/2,
                                (this->height()-30)/2,
                                QString("YOU DIED"));
               timer->stop();
    }

    QWidget::paintEvent(event);

}

